package com.zhizuobiao.coura.stockhelper;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

/**
 * Created by coura on 2017/1/3.
 */

public class SplashActivity extends AppCompatActivity {

    private static final String SHAREDPREFERENCES_NAME = "my_pref";
    private static final String KEY_GUIDE_ACTIVITY = "guide_activity";
    private final static int SWITCH_MAINACTIVITY = 1000;
    private final static int SWITCH_GUIDACTIVITY = 1001;
    // Handler:跳转至不同页面
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case SWITCH_MAINACTIVITY:
                    Intent intent1 = new Intent(SplashActivity.this,MainActivity.class);
                    SplashActivity.this.startActivity(intent1);
                    SplashActivity.this.finish();
                    break;
                case SWITCH_GUIDACTIVITY:
                    Intent intent2 = new Intent(SplashActivity.this,GuideActivity.class);
                    SplashActivity.this.startActivity(intent2);
                    SplashActivity.this.finish();
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_layout);
        boolean mFirst = isFirstEnter(this,getClass().getName());
        if (mFirst){
            handler.sendEmptyMessageDelayed(SWITCH_GUIDACTIVITY,3000);
        }else {
            handler.sendEmptyMessageDelayed(SWITCH_MAINACTIVITY,3000);
        }
    }

    /**
     * 判断应用是否初次加载，读取SharedPreferences中的guide_activity字段
     * @param context
     * @param className
     * @return
     */
    private boolean isFirstEnter(Context context,String className){
        if (context==null||className==null||"".equalsIgnoreCase(className)){
            return false;
        }
        String mResultStr = context.getSharedPreferences(SHAREDPREFERENCES_NAME,Context.MODE_WORLD_READABLE)
                .getString(KEY_GUIDE_ACTIVITY,"");
        return !mResultStr.equalsIgnoreCase("false");
    }
}
